<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Response;

class Image_ct extends Controller
{
    public function upload(Request $request){
        //判断是否是POST上传
        if ($request->isMethod('POST')) {
            //在源生的php代码中是使用$_FILE来查看上传文件的属性
            //但是在laravel里面有更好的封装好的方法，就是下面这个
            //显示的属性更多
            $fileCharater = $request->file('source');
//            dd($request->all());

            if ($fileCharater->isValid()) { //括号里面的是必须加的哦
                //如果括号里面的不加上的话，下面的方法也无法调用的

                //获取文件的扩展名
                $ext = $fileCharater->getClientOriginalExtension();
                //判断上传图片类型是否正确
                $img_class=array('.jpg','.png','.gif');
                if (in_array($ext,$img_class)){
                    //获取文件的绝对路径
                    $path = $fileCharater->getRealPath();

                    //定义文件名
                    $filename = date('Y-m-d-h-i-s').'.'.$ext;

                    //存储文件。disk里面的public。总的来说，就是调用disk模块里的public配置
                    Storage::disk('public')->put($filename, file_get_contents($path));
                    return array('info'=>$path,'name'=>$filename,'ext'=>$ext);
                }else{
                    return array('图片格式有误');
                }


            }
        }
//        return view('upload');
    }

    public function uploading(Request $request)
    {
        // 设置超时时间为没有限制
        ini_set("max_execution_time", "0");

        $http_type = ((isset($_SERVER['HTTPS'])&&$_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';

        $file = $request->file('file');

        $allowed_extensions = ["png", "jpg", "gif", "jpeg", "bmp"];
        if ($file->getClientOriginalExtension() && !in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
            return json_encode(['error' => 'You may only upload png, jpg or gif or jpeg or bmp.']);
        }

        $destinationPath = 'storage/uploads/'.date('Ymd').'/'; //public 文件夹下面建 storage/uploads 文件夹
        $extension = $file->getClientOriginalExtension();
        $fileName = md5(microtime(true)).'.'.$extension;
        $file->move($destinationPath, $fileName);

        return json_encode(['type' => $extension , 'url' => $http_type.$_SERVER['HTTP_HOST'].'/'.$destinationPath.'/'.$fileName , 'name' => $fileName]);
    }

    public function uploadimg(Request $request)
    {
        // 设置超时时间为没有限制
        ini_set("max_execution_time", "0");

        $http_type = ((isset($_SERVER['HTTPS'])&&$_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';

        $file = $request->file('file');

        $allowed_extensions = ["png", "jpg", "gif", "jpeg", "bmp"];
        if ($file->getClientOriginalExtension() && !in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
            return json_encode(['error' => 'You may only upload png, jpg or gif or jpeg or bmp.']);
        }

        $destinationPath = 'storage/uploads/'.date('Ymd').'/'; //public 文件夹下面建 storage/uploads 文件夹
        $extension = $file->getClientOriginalExtension();
        $fileName = md5(microtime(true)).'.'.$extension;
        $file->move($destinationPath, $fileName);

        return json_encode(['type' => $extension , 'url' => $http_type.$_SERVER['HTTP_HOST'].'/'.$destinationPath.'/'.$fileName , 'name' => $fileName]);
    }



}
